//
//  ECFormatHelper.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/29.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECFormatHelper.h"

@implementation ECFormatHelper

+ (NSAttributedString *)attributedStringForPromotionPrice:(NSString *)price decimal:(NSInteger)decimal smallFontSize:(CGFloat)size
{
    CGFloat priceNum = [price floatValue];
    if (priceNum <= 0) {
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"免费"];
//        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, 2)];
        [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#279af7"] range:NSMakeRange(0, 2)];

        return attributeString;
    }
    NSString *priceStr = [NSString stringWithFormat:@"¥ %@",price];
    switch (decimal) {
        case 0:
            priceStr = [NSString stringWithFormat:@"¥ %.0f",priceNum];
            break;
        case 1:
            priceStr = [NSString stringWithFormat:@"¥ %.1f",priceNum];
            
            break;
        case 2:
            priceStr = [NSString stringWithFormat:@"¥ %.2f",priceNum];
            
            break;
        default:
            break;
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, 1)];
    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#ef4023"] range:NSMakeRange(0, priceStr.length)];

    if ([priceStr containsString:@"."]) {
        NSInteger index = [priceStr rangeOfString:@"."].location;
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(index, priceStr.length - index)];
    }
    return attributeString;
}
+(NSAttributedString *)attributedStringForPromotionPrice:(NSString *)price decimal:(NSInteger)decimal;
{
    return [ECFormatHelper attributedStringForPromotionPrice:price decimal:2 smallFontSize:13 * kPercentage];

}
+(NSAttributedString *)attributedStringForOrginPrice:(NSString *) price decimal:(NSInteger) decimal{
    CGFloat priceNum = [price floatValue];
    
    NSString *priceStr = [NSString stringWithFormat:@"¥ %@",price];
    switch (decimal) {
        case 0:
            priceStr = [NSString stringWithFormat:@"¥ %.0f",priceNum];
            break;
        case 1:
            priceStr = [NSString stringWithFormat:@"¥ %.1f",priceNum];
            
            break;
        case 2:
            priceStr = [NSString stringWithFormat:@"¥ %.2f",priceNum];
            
            break;
        default:
            break;
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:priceStr];
    
//    if ([priceStr containsString:@"."]) {
//        NSInteger index = [priceStr rangeOfString:@"."].location;
//        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10 * kPercentage] range:NSMakeRange(index, priceStr.length - index)];
        [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, priceStr.length)];

        
//    }
    ////                                            NSStrikethroughColorAttributeName : UIColorFromHexValue(0xBBBBBB),
    [attributeString addAttributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),
                                    
                                     NSBaselineOffsetAttributeName:@(0)} range:NSMakeRange(0, priceStr.length)];
//
    return attributeString;
}

@end
